import Vue from 'vue'
import VueRouter from 'vue-router'
import Movie from '../views/home/Movie.vue'
//使用路由插件
Vue.use(VueRouter)
//定义路由表
const routes = [
  {path:"/",redirect: "/home"},
  {
    path: '/home',
    name: 'Movie',
    component: Movie,
    children:[
      {path:"/home",redirect: "/home/reying"},
      {path:"/home/reying",name:"reying", component: () => import(/* webpackChunkName: "reying" */ '../views/home/reying.vue')},
      {path:"/home/yingyuan",name:"yingyuan", component: () => import(/* webpackChunkName: "yingyuan" */ '../views/home/yingyuan.vue')},
      {path:"/home/daiying",name:"daiying", component: () => import(/* webpackChunkName: "daiying" */ '../views/home/daiying.vue')},
      {path:"/home/jingdian",name:"jingdian", component: () => import(/* webpackChunkName: "jingdian" */ '../views/home/jingdian.vue')},
    ]
  },
  {
    path: '/video',
    name: 'Video',
    //路由懒加载 ，作用：优化性能，点击后显示这个页面时才加载页面内容
    component: () => import(/* webpackChunkName: "video" */ '../views/Video.vue')
  },
  {
    path: '/minivideo',
    name: 'MiniVideo',
    component: () => import(/* webpackChunkName: "minivideo" */ '../views/MiniVideo.vue')
  },
  {
    path: '/show',
    name: 'Show',
    component: () => import(/* webpackChunkName: "show" */ '../views/Show.vue')
  },
  {
    path: '/me',
    name: 'Me',
    component: () => import(/* webpackChunkName: "me" */ '../views/Me.vue')
  },
  {
    path: '/item/:id',//动态路由
    name: 'Item',
    component: () => import(/* webpackChunkName: "item" */ '../views/Item.vue')
  },
  {
    path: '/login',//动态路由
    name: 'Login',
    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '/city',//动态路由
    name: 'City',
    component: () => import(/* webpackChunkName: "city" */ '../views/City.vue')
  },
  {//404路由
    path: '/*',
    name: 'page404',
    component: () => import(/* webpackChunkName: "page404" */ '../views/Page404.vue')
  }
]
//创建路由对象
const router = new VueRouter({
  model:"hash",//history:没有#号，需要后台支持
  routes
})

//添加全局守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/me'){
    if(sessionStorage.getItem('loginType')){
      next();
    }else{
      next('/login');
    }
  }else{
    next();
  }
})

//输出
export default router